﻿namespace CatClient.Message.Spi.Heartbeat
{
    using CatClient.Util;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Xml.Serialization;

    [XmlRoot("runtime")]
    public class RuntimeInfo : IRefresh
    {
        private readonly Stopwatch _stopwatch = new Stopwatch();

        public RuntimeInfo()
        {
            this._stopwatch.Start();
            this.StartTime = DateTime.Now.Ticks;
            this.UserName = Environment.UserName;
            this.AppPath = AppDomain.CurrentDomain.BaseDirectory;
            this.NetVersion = Environment.Version.ToString();
            this._stopwatch.Stop();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info($"Heartbeat {"RuntimeInfo"} init finish. {this._stopwatch.ElapsedMilliseconds}ms");
        }

        public void Refresh()
        {
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info($"Heartbeat {"RuntimeInfo"} refreshed.");
        }

        [XmlAttribute("start-time")]
        public long StartTime { get; set; }

        [XmlAttribute("up-time")]
        public long UpTime { get; set; }

        [XmlAttribute("java-version")]
        public string NetVersion { get; set; }

        [XmlAttribute("user-name")]
        public string UserName { get; set; }

        [XmlElement("user-dir")]
        public string AppPath { get; set; }

        [XmlElement("java-classpath")]
        public string ClasPath { get; set; }
    }
}

